package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/


import com.sound.ampache.objects.Album;
import com.sound.ampache.objects.Artist;

import net.rim.device.api.collection.util.LongHashtableCollection;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;


class DataStore {
	   
	private static PersistentObject store;
	private static DataStore instance;
	private LongHashtableCollection settings;
   
	private static final long KEY_APP_SRV_URL = 0;
	private static final long KEY_USERNAME = 1;
	private static final long KEY_PASSWORD = 2;
	private static final long KEY_CACHE_VAL = 3;
	private static final long KEY_UPDATE_VAL = 4;
	private static final long KEY_CACHED_ALBUMS = 5;
	private static final long KEY_CACHED_ARTISTS = 6;
	private static final long KEY_FONT_SIZE = 7;
	private static final long KEY_CONN_TYPE = 8;
   
	private static String DEFAULT_SERVICE_URL = "";
	private static String DEFAULT_USERNAME = "";
	private static String DEFAULT_PASSWORD = "";
	private static Boolean DEFAULT_CACHE_VAL = new Boolean(false);
	private static Boolean DEFAULT_UPDATE_VAL = new Boolean(false);
	private static String DEFAULT_FONT_SIZE = "18";
	private static String DEFAULT_CONN_TYPE = "Auto";
	   
	   
	private static DataStore getInstance() {
		if (null == instance) {
			instance = new DataStore();
		}
		return instance; 
	}
	    
	   
	private DataStore() {
		//Hash of ampacheberry
		store = PersistentStore.getPersistentObject(0x3609048ebae542d0L);
	}
	    
	    
	public static String getAppServerUrl() {
		String url = (String)getInstance().get(KEY_APP_SRV_URL);
		if (null == url || url.length() == 0) {
			url =  DEFAULT_SERVICE_URL; 
			setAppServerUrl(url);
		}
		return url;
	}
	   
	    
	public static void setAppServerUrl(String url) {
		getInstance().set(KEY_APP_SRV_URL,url);
	} 
	    
	    
	public static String getUserName() {
		String username = (String)getInstance().get(KEY_USERNAME);
		if (null == username || username.length() == 0) {
			username =  DEFAULT_USERNAME; 
			setUsername(username);
		}
		return username;
	}
		 
	
	public static void setUsername(String username) {
		getInstance().set(KEY_USERNAME,username);
	} 
	
	
	public static String getPassword() {
		String password = (String)getInstance().get(KEY_PASSWORD);
		if (null == password || password.length() == 0) {
			password =  DEFAULT_PASSWORD; 
			setPassword(password);
		}
		return password;
	}
		 
	
	public static void setPassword(String password) {
		getInstance().set(KEY_PASSWORD,password);
	} 
	
	
	public static Boolean getCacheVal() {
		Boolean cacheVal = (Boolean)getInstance().get(KEY_CACHE_VAL);
		if (null == cacheVal) {
			cacheVal =  DEFAULT_CACHE_VAL; 
			setCacheVal(cacheVal);
		}
		return cacheVal;
	}
	
	
	public static void setCacheVal(Boolean cacheVal) {
		getInstance().set(KEY_CACHE_VAL,cacheVal);
	} 
	
	
	public static Boolean getUpdateVal() {
		Boolean updateVal = (Boolean)getInstance().get(KEY_UPDATE_VAL);
		if (null == updateVal) {
			updateVal =  DEFAULT_UPDATE_VAL; 
			setUpdateVal(updateVal);
		}
		return updateVal;
	}
	
	
	public static void setUpdateVal(Boolean updateVal) {
		getInstance().set(KEY_UPDATE_VAL,updateVal);
	} 
		 
	  
	public static Album[] getCachedAlbums() {
		Album[] albums = (Album[])getInstance().get(KEY_CACHED_ALBUMS);
		if (null == albums ) {
			albums =  new Album[0]; 
			setCachedAlbums(albums);
		}
		return albums;
	}
	    
	
	public static void setCachedAlbums(Album[] albums) {
		getInstance().set(KEY_CACHED_ALBUMS, albums);
	}  
	
	
	public static void clearCachedAlbums() {
		getInstance().set(KEY_CACHED_ALBUMS, new Album[0]);
	} 
	
	
	public static Artist[] getCachedArtists() {
		Artist[] artists = (Artist[])getInstance().get(KEY_CACHED_ARTISTS);
		if (null == artists ) {
			artists =  new Artist[0]; 
			setCachedArtists(artists);
		}
		return artists;
	}
	    
	
	public static void setCachedArtists(Artist[] artists) {
		getInstance().set(KEY_CACHED_ARTISTS, artists);
	}  
	
	
	public static void clearCachedArtists() {
		getInstance().set(KEY_CACHED_ARTISTS, new Artist[0]);
	} 
	
	
	public static String getFontSize() {
		String fontSize = (String)getInstance().get(KEY_FONT_SIZE);
		if (null == fontSize || fontSize.length() == 0) {
			fontSize =  DEFAULT_FONT_SIZE; 
			setFontSize(fontSize);
		}
		return fontSize;
	}
		 
	
	public static void setFontSize(String fontSize) {
		getInstance().set(KEY_FONT_SIZE,fontSize);
	} 
	
	
	public static String getConnType() {
		String connType = (String)getInstance().get(KEY_CONN_TYPE);
		if (null == connType || connType.length() == 0) {
			connType =  DEFAULT_CONN_TYPE; 
			setConnType(connType);
		}
		return connType;
	}
		 
	
	public static void setConnType(String connType) {
		getInstance().set(KEY_CONN_TYPE,connType);
	} 
	    
	    
	private void set(long key, Object value) {
		synchronized(store) {
			settings = (LongHashtableCollection)store.getContents();
			if (null == settings) {
				settings = new LongHashtableCollection();
			}
			settings.put(key,value);   
			store.setContents(settings);
			store.commit();
		}
	}

	
	private Object get(long key) {
		synchronized(store) {
			settings = (LongHashtableCollection)store.getContents();
			if (null != settings && settings.size() != 0) {
				return settings.get(key);
			} else {
				return null;
			}
		}
	}
	   
} 